package com.jason.platform.supervision.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;
import com.jason.platform.supervision.beans.LoginReq;
import com.jason.platform.supervision.beans.UserInfoBean;
import com.jason.platform.supervision.task.HttpConfig;

import java.io.*;

/**
 * @ClassName: SharedPreferencesUtils
 * @Description: 本地缓存
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015-7-27 09:55:39
 */
public class SharedPreferencesUtils {

    //用户信息
    private static final String USER_INFO = "user_info";
    //用户信息
    public static final String KEY_USER_INFO = "key_user_info";
    //登录信息
    public static final String KEY_LOGIN_INFO = "key_login_info";
    //记住密码
    private static final String KEY_REMIND_PASSWORD = "key_remind_password";
    //机构id
    private static final String KEY_ORGAN_ID = "key_organ_id";
    //网络配置ip
    private static final String KEY_HTTP_HOST = "key_http_host";
    //网络配置端口
    private static final String KEY_HTTP_PORT = "key_http_port";


    /***
     * 保存ip
     * @param context
     * @param value
     */
    public static void setHttpHost(Context context, String value){
        SharedPreferences sp = context.getSharedPreferences(USER_INFO, Context.MODE_PRIVATE);
        sp.edit().putString(KEY_HTTP_HOST, value).commit();
    }

    //获取当前ip
    public static String getHttpHost(Context context){
        SharedPreferences sp = context.getSharedPreferences(USER_INFO, Context.MODE_PRIVATE);
        return sp.getString(KEY_HTTP_HOST, HttpConfig.HOST);
    }

    /***
     * 保存端口
     * @param context
     * @param value
     */
    public static void setHttpPort(Context context, String value){
        SharedPreferences sp = context.getSharedPreferences(USER_INFO, Context.MODE_PRIVATE);
        sp.edit().putString(KEY_HTTP_PORT, value).commit();
    }

    //获取当前端口
    public static String getHttpPort(Context context){
        SharedPreferences sp = context.getSharedPreferences(USER_INFO, Context.MODE_PRIVATE);
        return sp.getString(KEY_HTTP_PORT, HttpConfig.PORT);
    }

    /***
     * 设置是否保存密码
     * @param context
     * @param value
     */
    public static void setRemindPassword(Context context, boolean value){
        SharedPreferences sp = context.getSharedPreferences(USER_INFO, Context.MODE_PRIVATE);
        sp.edit().putBoolean(KEY_REMIND_PASSWORD, value).commit();
    }

    //获取保存密码状态
    public static boolean getRemindPassword(Context context){
        SharedPreferences sp = context.getSharedPreferences(USER_INFO, Context.MODE_PRIVATE);
        return sp.getBoolean(KEY_REMIND_PASSWORD, false);
    }

    /***
     * 设置是否保存密码
     * @param context
     * @param value
     */
    public static void setOrganId(Context context, Integer value){
        SharedPreferences sp = context.getSharedPreferences(USER_INFO, Context.MODE_PRIVATE);
        sp.edit().putInt(KEY_ORGAN_ID, value).commit();
    }

    //获取保存密码状态
    public static Integer getOrganId(Context context){
        SharedPreferences sp = context.getSharedPreferences(USER_INFO, Context.MODE_PRIVATE);
        return sp.getInt(KEY_ORGAN_ID, 0);
    }

    /**
     * 保存用户信息到手机文件中
     *
     * @param context
     * @param userInfo
     */
    public static void setUserInfoBean(Context context, UserInfoBean userInfo) {
        SharedPreferences preferences = context.getSharedPreferences(USER_INFO, Context.MODE_PRIVATE);
        ByteArrayOutputStream baos = null;
        ObjectOutputStream oos = null;
        try {
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(userInfo);
            String login = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString(KEY_USER_INFO, login);
            editor.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (baos != null) {
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public static UserInfoBean getUserInfoBean(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(USER_INFO, Context.MODE_PRIVATE);
        String login = preferences.getString(KEY_USER_INFO, "");
        byte[] base64Bytes = Base64.decode(login.getBytes(), Base64.DEFAULT);
        ByteArrayInputStream bais = null;
        ObjectInputStream ois = null;
        UserInfoBean userInfo = null;
        try {
            bais = new ByteArrayInputStream(base64Bytes);
            ois = new ObjectInputStream(bais);
            userInfo = (UserInfoBean) ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bais != null) {
                try {
                    bais.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return userInfo;

    }

    /**
     * 保存用户登录信息
     *
     * @param context
     * @param loginReq
     */
    public static void setLoginInfo(Context context, LoginReq loginReq) {
        SharedPreferences preferences = context.getSharedPreferences(USER_INFO, Context.MODE_PRIVATE);
        ByteArrayOutputStream baos = null;
        ObjectOutputStream oos = null;
        try {
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(loginReq);
            String login = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString(KEY_LOGIN_INFO, login);
            editor.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (baos != null) {
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    /****
     * 获取用户登录信息
     * @param context
     * @return
     */
    public static LoginReq getLoginInfo(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(USER_INFO, Context.MODE_PRIVATE);
        String login = preferences.getString(KEY_LOGIN_INFO, "");
        byte[] base64Bytes = Base64.decode(login.getBytes(), Base64.DEFAULT);
        ByteArrayInputStream bais = null;
        ObjectInputStream ois = null;
        LoginReq loginReq = null;
        try {
            bais = new ByteArrayInputStream(base64Bytes);
            ois = new ObjectInputStream(bais);
            loginReq = (LoginReq) ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bais != null) {
                try {
                    bais.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return loginReq;

    }
}
